<?php

namespace Modules\Order\Exports;

use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithTitle;

use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;

// use PHPExcel_Cell_DataType;
use PhpOffice\PhpSpreadsheet\Cell\DataType;

use Modules\Order\Entities\Order;

class OrderExport implements WithColumnFormatting, FromCollection, WithTitle, WithHeadings, WithMapping, ShouldAutoSize
{
    use Exportable;

    private $list;

    public function __construct($list)
    {
        $this->list = $list;
    }

    public function columnFormats(): array
    {
        return [];
        // return [
        //     'O' => DataType::TYPE_STRING
        // ];
    }

    /**
     * @return Builder
     */
    public function collection()
    {
        return $this->list;
        // return BookInfo::where('booking_session_id', $this->session->id)->displayed();
    }

    /**
     * @return string
     */
    public function title(): string
    {
        return '订单导出';
    }

    public function map($order): array
    {
        $addressStr = '';
        $name = '';
        $phone = '';

        if ($order->address) {
            $a = $order->address;
            $name = $order->address->consignee;
            $phone = $order->address->phone;
            $addressStr .= $a->province . ' ' . $a->city . ' ' . $a->district . ' ' . $a->address;
        }

        $expressInc = '';
        $expressNumber = '';

        $deliverAt = '';
        $deliverRemark = '';

        if ($order->deliver_record) {
            $expressNumber = $order->deliver_record->express_number . ' ';
            $expressInc = $order->deliver_record->express_name;

            $deliverAt = $order->deliver_record->created_at;
            $deliverRemark = $order->deliver_record->remark;
        }



        $res = [
            [
                $order->id,
                $order->number,
                @$order->user->name,
				@$order->user->phone,
                $order->created_at,
                $order->money_paid_at,
                $order->score_paid_at,
                $order->product_money_price / 100,
                $order->product_score_price,
                $order->pay_money_price / 100,
                $order->pay_score_price,
                $order->score_used,
                $order->score_discount / 100,
                $order->redpack_discount / 100,
                @$order->sku->title,
                @$order->sku->number,
                @$order->sku->generateAttrStr(),
                @$order->sku->discount_money_price / 100,
                // @$order->sku->selling_score_price,
                @$order->sku->total,
                @$order->sku->supplier,
                @$order->sku->cost_money_price / 100,
                // @$order->sku->selling_price / 100,
                $order->union_status_text,
                $order->miniapp_type === 'ali' ? '天猫' : '微信',
                $name,
                $phone,
                $addressStr,
                $deliverAt,
                $expressInc,
                $expressNumber,
                $deliverRemark,
                $order->seller_remark
            ]
        ];

        $skus = $order->skus;
        $skus->shift();

        foreach ($skus as $sku) {
            array_push($res, [
                '',
                '',
                '',
                '',
                '',
                '',
                '',
                '',
                '',
                '',
                '',
                '',
                '',
                $sku->title,
                $sku->number,
                $sku->generateAttrStr(),
                $sku->discount_money_price / 100,
                // $sku->selling_score_price,
                $sku->total,
                $sku->supplier,
                $sku->cost_money_price / 100,
                // $sku->selling_price / 100,
                $order->union_status_text,
                $order->miniapp_type === 'ali' ? '天猫' : '微信',
                $name,
                $phone,
                $addressStr,
                $deliverAt,
                $expressInc,
                $expressNumber,
                $deliverRemark,
                $order->seller_remark
            ]);
        }


        return $res;
    }

    public function headings(): array
    {
        return [
            '唯一ID',
            '编号',
            '用户名',
			'用户电话',
            '下单时间',
            '人民币支付时间',
            '积分支付时间',
            '商品总价',
            '积分总价',
            '人民币实付',
            '积分实付',
            '积分抵扣数量',
            '积分抵扣金额',
            '红包抵扣金额',
            '商品标题',
            '商品编号',
            '商品属性',
            // 'SKU编号',
            '商品单价(人民币)',
            // '商品单价(积分)',
            '数量',
            '供应商名称',
            '成本价',
            // '支付金额',
            '状态',
            '订单来源',
            '收件人姓名',
            '收件人电话',
            '收件人地址',
            '发货时间',
            '快递公司',
            '快递单号',
            '发货备注',
            '卖家备注'
        ];
    }
}
